package enumdemo;

public enum TestEnum {
    //枚举对象
    RED("red",0),
    BLACK("black",1),
    GREEN("green",2),
    WHITE("white",3),
    YELLOW("yellow",4);
    public String color;
    public int ordinal;
    //枚举的构造方法默认是私有的
    TestEnum (String color,int ordinal) {
        this.color = color;
        this.ordinal = ordinal;
    }

    public static void main(String[] args) {
        /*values()在父类Enum中并没有,
        values()方法是编译器插入到enum定义中的static方法,
        所以，当你将enum实例向上转型为父类Enum时，values()就不可访问了
         */
        TestEnum[] testEnum = TestEnum.values();
        for (int i = 0; i < testEnum.length; i++) {
            System.out.println(testEnum[i]+"->"+testEnum[i].ordinal());
        }
        System.out.println("=========================");
        System.out.println(TestEnum.valueOf("GREEN"));
        System.out.println(RED.compareTo(GREEN));//拿ordinal序号比

    }
    public static void main1(String[] args) {
        TestEnum testEnum = TestEnum.BLACK;
        switch (testEnum) {
            case RED: System.out.println("red");
                break;
            case BLACK: System.out.println("black");
                break;
            case WHITE: System.out.println("white");
                break;
            case GREEN: System.out.println("green");
                break;
            default:
                break;
        }
    }
}
